{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据类型\n",
    "### 有符号整数\n",
    "对于有符号整数位，第一个bit是0则是正数，第一个bit是1则是负数。\n",
    "8个bit位理应能存储256个状态。但是正常存储的话只能表示[-127, 127]这255个状态，因为0000 0000和1000 0000这两个状态都表示0，浪费了一个状态。为了避免浪费，提出了补码。\n",
    "\n",
    "\n",
    "每一个0开头的正整数都有一个对应的反码来表示它的相反数，例如：\n",
    "\n",
    "源码：0111 1111 ---> 127\n",
    "\n",
    "反码：1000 0000\n",
    "\n",
    "加一：1000 0001 --->-127的存储方法\n",
    "\n",
    "计算机看到1开头的1000 0001时，知道它是负数，便会减1取反得到源码看是多少。\n",
    "0000 0000取反加1之后还是0000 0000。因此[0, 127]取反后能得到[-127, 127]这255个数。但是还有一个1000 0000没有被利用到，对1000 0000减1取反得到的是128，因此8位有符号整数位ie表示范围为[-128, 127]这256个状态。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 取模、取整运算\n",
    "1. 取模：【原数】减去 【比他小的最大的模板的倍数】\n",
    "注意此处，比它小在负数时，绝对值会更大。\n",
    "2. 取整：【原数】通过减去一个数变成 【比他小的最大的模板的倍数】\n",
    "1. 正整数\n",
    "整除取余\n",
    "2. 负整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "-2\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "#正整数相当于：5 - 1*3 = 2，也就是找到比5小的3的倍数\n",
    "print( 5 % 3 )\n",
    "#负整数相当于：-5 - (-2 * 3) = 1，也就是找到比-5小的3的倍数，也可以理解为：2*3 - 5 = 1\n",
    "print( -5 % 3 )\n",
    "# 整除取整同理 \n",
    "print( -5 // 3)\n",
    "print(-23 % 10)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3.6.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
